package com.aw.baselib.net.api;

import android.util.Log;


import com.aw.baselib.util.AwLog;
import com.aw.baselib.util.UserUtil;

import java.io.IOException;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by hzw
 */

public class UrlInterceptor implements Interceptor {

    private static final String TAG = "UrlInterceptor";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Log.d(TAG, "UrlInterceptor_ " + System.currentTimeMillis());
        Request oldRequest = chain.request();
        // 添加新的参数
        HttpUrl.Builder authorizedUrlBuilder = oldRequest.url()
                .newBuilder()
                .scheme(oldRequest.url().scheme())
                .host(oldRequest.url().host());
        AwLog.d(TAG, "请求时的token: " + UserUtil.getToken());
        // 新的请求
        Request newRequest = oldRequest.newBuilder()
                .addHeader("Accept-Encoding", "gzip")
                .addHeader("Accept", "application/json")
                .addHeader("Content-Type", "application/json; charset=utf-8")
                .addHeader("Authorization", UserUtil.getToken())
                .method(oldRequest.method(), oldRequest.body())
                .url(authorizedUrlBuilder.build())
                .build();
        AwLog.d(TAG, newRequest.toString());
        //响应信息拦截
        return chain.proceed(newRequest);
    }
}
